<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * Hockey extension for Joomleague
 * @version $Id: HockeyProject.php 132 2008-09-18 08:55:06Z julien.vonthron $
 * @package JlgHockeyExtension
 * @copyright Copyright (C) 2007 Julien Vonthron. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * JlgHockeyExtension is free software and parts of it may contain or be derived from the
 * GNU General Public License or other free or open source software licenses.
 * 
 * contact: julien.vonthron[at]gmail.com 
 *   
 * Joomleague is a League Management and Prediction Game for Joomla!
 * Homepage: http://www.joomleague.de 
 *  
 */

class HockeyProject extends JoomLeague{
  
	/**
	 * gives games played/came in/came out stats for player
	 *
	 * @param int $player_id
	 * @param int $project_id
	 * @param int $team_id
	 * @param int $position_id (if null, all positions)
	 * @return Object (members: played, subst, sub_out)
	 */
  function getStartInOutStats($player_id, $project_id, $team_id, $position_id = 0) 
  {
    global $database;
    $query = "SELECT count(mp.id) as played, sum(mp.came_in) as subst, sum(mp.out = 1) AS sub_out
              FROM #__joomleague_match_players AS mp 
              INNER JOIN #__joomleague_matches AS m ON mp.match_id = m.match_id 
              WHERE mp.player_id = '$player_id' 
                AND m.project_id = '$project_id'
                AND mp.team_id = '$team_id'";
    if ($position_id) {
      $query .= ' AND mp.position_id = ' . $position_id;
    }
    $database->setQuery($query);
    $result = NULL;
    $database->loadObject($result);
    return $result;
  }
  
  /**
   * return stat for specified player id and eventtype id
   *
   * @param int $pid player id
   * @param int $evid eventtype id
   * @param int $team_id 
   * @return unknown
   */
  function getPlayerEvents($pid, $evid, $team_id) 
  {
    return HockeyProject::getPlayerEventStat($pid, $evid, $this->id, $team_id);
  }

  /**
   * return stat for specified player id and eventtype id
   *
   * @param int $pid player id
   * @param int $evid eventtype id
   * @param int $project_id 
   * @param int $team_id 
   * @return unknown
   */
  function getPlayerEventStat($pid, $evid, $project_id, $team_id) 
  {
    global $database, $overall_config;
    $playerpoints_eventId = $overall_config['event_points'];
    $goals_eventId = $overall_config['event_goals'];
    $assist_eventId = $overall_config['event_assists'];
    //$games_eventId = $overall_config['event_goalie_games'];
    $goalsAllowed_eventId = $overall_config['event_goals_allowed'];
    $goalsAgAvg_eventId = $overall_config['event_goalie_gagavg'];
    $shutouts_eventId = $overall_config['event_goalie_shutouts'];
    $posGoalie = $overall_config['goalie_position_id'];
    
    switch ($evid) {
      case $playerpoints_eventId:
        $query = "SELECT SUM( event_sum )
                  FROM #__joomleague_match_events AS me
                  INNER JOIN #__joomleague_match_players AS mp USING (match_id, player_id)
                  WHERE me.player_id = $pid
                    AND (event_type_id = $goals_eventId OR event_type_id = $assist_eventId) 
                    AND project_id = $project_id
                    AND mp.team_id=$team_id";
        $database->setQuery($query);
        return $database->loadResult();
        break;
      case $goalsAgAvg_eventId:
      	// sub request for games played
			  $sub_games_played = "SELECT COUNT(*) AS games_played
			                        FROM #__joomleague_match_players AS mp
			                        INNER JOIN #__joomleague_matches AS m USING (match_id)
			                        WHERE mp.player_id = $pid
			                          AND mp.team_id=$team_id
			                          AND m.project_id = $project_id
			                          AND mp.position_id = $posGoalie			                          
			                        GROUP BY mp.player_id";
			  
			  // sub request for goals allowed
			  $sub_goals_allowed = "SELECT SUM(me.event_sum) AS goals_allowed
						                  FROM #__joomleague_match_events AS me
						                  INNER JOIN #__joomleague_match_players AS mp USING (match_id, player_id)
						                  WHERE me.player_id = $pid
						                    AND me.event_type_id = $goalsAllowed_eventId 
						                    AND me.project_id = ".$project_id."
						                    AND mp.team_id=$team_id";
			  
        $query = "SELECT games_played, goals_allowed
                  FROM ($sub_games_played) AS gp, ($sub_goals_allowed) AS ga";
        $database->setQuery($query);
        if ($result = $database->loadRow()) {
          if ($result[0]) return $result[1]/$result[0];
          else return 0;
        }
//        else {        	
//	        echo $database->getQuery($query);
//	        echo $database->getErrorMsg();
//        }
        break;
      case $shutouts_eventId:
        $query = "SELECT COUNT(*)
                  FROM #__joomleague_match_players AS mp
                  INNER JOIN #__joomleague_matches AS m USING (match_id)
                  LEFT JOIN #__joomleague_match_events AS me 
                    ON (mp.match_id = me.match_id 
                    AND mp.player_id = me.player_id
                    AND event_type_id = $goalsAllowed_eventId)
                  WHERE mp.player_id = $pid
                  AND (event_sum IS NULL OR event_sum=0)
                  AND m.project_id = $project_id
                  AND mp.position_id = $posGoalie";
        $database->setQuery($query);
        //echo $database->getQuery($query);
        return $database->loadResult();
        break;
      default:
        $query = "SELECT SUM(me.event_sum)
                  FROM #__joomleague_match_events AS me
                  INNER JOIN #__joomleague_match_players AS mp USING (match_id, player_id)
                  WHERE me.player_id = $pid
                    AND me.event_type_id = $evid 
                    AND me.project_id = ".$project_id."
                    AND mp.team_id=$team_id";
        $database->setQuery($query);
        //echo $database->getQuery($query);
        return $database->loadResult();
        break;
    }
    return $result;
  }
  
}	
?>
